package com.xgst.readbook.base

import android.content.Intent
import android.content.res.Resources
import android.database.DatabaseUtils
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewStub
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.AdaptScreenUtils
import com.xgst.readbook.R

abstract class BaseActivity<VB : ViewBinding> :AppCompatActivity() {

    private var _binding : VB? = null
    protected val binding: VB
        get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = viewBinding()
        val resId = getLayoutId()
        if(resId != -1){
            setContentView(R.layout.view_stub_layout)
            val viewStub = findViewById<ViewStub>(R.id.viewStub)
            viewStub.postDelayed({
                _binding = viewBinding()
                viewStub.layoutResource = getLayoutId()
                viewStub.inflate()
            }, 300)
            viewStub.setOnInflateListener { stub, inflated ->
                findViewById<View>(R.id.tvViewStubProgress).visibility = View.GONE
                findViewById<View>(R.id.imgViewStubProgress).visibility = View.GONE
                step()
            }
        }else{
            _binding = viewBinding()
            setContentView(binding.root)
            step()
        }
    }

    @LayoutRes
    open fun getLayoutId():Int  = -1

    abstract fun viewBinding(): VB

    private fun step(){
        val imgBack = findViewById<View>(R.id.imgBack)
        //返回
        imgBack?.setOnClickListener { finish() }
        init()
    }
    abstract fun init()

    override fun getResources(): Resources {
        return AdaptScreenUtils.adaptWidth(super.getResources(), 800)
    }

    fun startActivity(clazz: Class<*>){
        startActivity(Intent(this,clazz))
    }
}